package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import aad.storyboard.model.Script;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.DateCell;
 
public class ScriptParser {
        
        static ArrayList <Script> scripts=new ArrayList <Script> ();
 
        public void init(String filePath) {
                FileInputStream fs = null;
                try {
                        fs = new FileInputStream(new File(filePath));
                        contentReading(fs);
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (Exception e) {
                        e.printStackTrace();
                }finally {
                        try {
                                fs.close();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }
 
        //Returns the Headings used inside the excel sheet
        public void getHeadingFromXlsFile(Sheet sheet) {
                int columnCount = sheet.getColumns();
                for (int i = 0; i < columnCount; i++) {
                        System.out.println(sheet.getCell(i, 0).getContents());
                }
        }
 
        public void contentReading(InputStream fileInputStream) {
                WorkbookSettings ws = null;
                Workbook workbook = null;
                Sheet s = null;
                Cell rowData[] = null;
                int rowCount = 0;
                int columnCount = 0;
                DateCell dc = null;
                int totalSheet = 0;
 
                try {
                        ws = new WorkbookSettings();                    
                        workbook = Workbook.getWorkbook(fileInputStream, ws);
 
                        totalSheet = workbook.getNumberOfSheets();
                         
                        //Getting Default Sheet i.e. 0
                        s = workbook.getSheet(0);
 
                        //Reading Individual Cell
                        getHeadingFromXlsFile(s);
 
                        columnCount = s.getColumns();
                        rowCount=s.getRows();
 
                        //Reading Individual Row Content
                        for (int i = 1; i < rowCount; i++) {
                                //Get Individual Row
                                rowData = s.getRow(i);
                                Script scr=new Script();
                                if (rowData[0].getContents().length() != 0) { // the first date column must not null
                                        for (int j = 0; j < columnCount; j++) {
                                                if (j==1)                                               {
                                                        
                                                        scr.setText(rowData[j].getContents());
                                                }
                                                if (j==2)
                                                {
                                                        int time=ConvStdTimeToSec(rowData[j].getContents());                                                    
                                                        scr.setStartTime(time);
                                                        //System.out.println("Subttitle: "+scr.getText()+time);
                                                }
                                                if (j==3){
                                                        int t=ConvStdTimeToSec(rowData[j].getContents());                                                       
                                                        scr.setEndTime(t);
                                                        System.out.println("Subttitle: "+scr.getText()+scr.getStartTime()+"EndTime: "+t);
                                                }
                                        }
                                        scripts.add(scr);                                       
                                }
                        }
                        workbook.close();                       
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (BiffException e) {
                        e.printStackTrace();
                }
        }
 
                public static ArrayList <Script> initialiseSubtitleTimes(InputStream fileInputStream, ScriptParser xlReader )
                {
                try {                   
                        xlReader.contentReading(fileInputStream);                       
                        return scripts;
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
                
                private int ConvStdTimeToSec(String time)
                {
                return 
                (Integer.parseInt(time.substring(0,1)) * 60000 + Integer.parseInt(time.substring(2,4))*100);
                }
}
